local cwh_diy = require "packages.sanshe.pkg.utility.cwh_diy"
local DIY = require "packages.diy_utility.diy_utility"

local skel = fk.CreateSkill{
  name = "sj_zhoushuhuizhan__qianheanyiting",
  tags = {cwh_diy.ReadySkill},
}

Fk:loadTranslationTable{
  ["sj_zhoushuhuizhan__qianheanyiting"] = "嵌合暗翳庭",
  [":sj_zhoushuhuizhan__qianheanyiting"] = "<b><a href='cwh_diy_ready_skill'>蓄势技</a></b>，出牌阶段，你可以令所有其他角色选择：<br>"..
    "1.摸两张牌并翻面；2.弃置两张牌并隐匿，最后你可以失去1点体力交换两名角色的选择。本技能初始状态视为已发动过。",

  ["#sj_zhoushuhuizhan__qianheanyiting"] = "嵌合暗翳庭：你可以令所有其他角色选择摸两张牌并翻面或弃置两张牌并隐匿",

  ["#sj_zhoushuhuizhan__qianheanyiting_1"] = "摸两张牌并翻面",
  ["#sj_zhoushuhuizhan__qianheanyiting_2"] = "弃置两张牌并隐匿",
  ["#sj_zhoushuhuizhan__qianheanyiting_draw"] = "%dest 摸两张牌并翻面",
  ["#sj_zhoushuhuizhan__qianheanyiting_dis"] = "%dest 弃置两张牌并隐匿",

  ["#sj_zhoushuhuizhan__qianheanyiting_change"] = "嵌合暗翳庭：你可以失去1点体力，交换两名角色的选择",
  ["#sj_zhoushuhuizhan__qianheanyiting_choose"] = "嵌合暗翳庭：你须弃置两张牌并隐匿或摸两张牌并翻面",

  ["$sj_zhoushuhuizhan__qianheanyiting1"] = "",
  ["$sj_zhoushuhuizhan__qianheanyiting2"] = "",
}

Fk:addTargetTip{
  name = skel.name,
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    for _, v in ipairs(extra_data.extra_data.players_choice) do --双层
      if to_select.id == v[1] then  -- 什么时候才能传player进来
        return v[2]
      end
    end
  end,
}

skel:addEffect("active", {
  prompt = "#sj_zhoushuhuizhan__qianheanyiting",
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local players_choice,players_ask = {},{}
    for _, t in ipairs(room:getOtherPlayers(player)) do
      if #table.filter(t:getCardIds("he"),function (id)
        return not t:prohibitDiscard(id)
      end) > 1 then
        table.insert(players_ask,t)
      end
      players_choice[t] = "#sj_zhoushuhuizhan__qianheanyiting_1"
    end
    if #players_ask > 0 then
      local tc = room:askToJointChoice(player,{
        players = players_ask,
        choices = {"#sj_zhoushuhuizhan__qianheanyiting_1","#sj_zhoushuhuizhan__qianheanyiting_2"},
        skill_name = skel.name,
        prompt = "#sj_zhoushuhuizhan__qianheanyiting_choose",
      })
      for t, c in pairs(tc) do
        for target, choice in pairs(players_choice) do
          if t.id == target.id then
            players_choice[target] = c
          end
        end
      end
    end
    local list = {}
    for target, choice in pairs(players_choice) do
      local c = "#sj_zhoushuhuizhan__qianheanyiting_draw::"..target.id
      if choice == "#sj_zhoushuhuizhan__qianheanyiting_2" then
        c = "#sj_zhoushuhuizhan__qianheanyiting_dis::"..target.id
      end
      table.insert(list,c)
    end
    if #list > 1 then
      local target_tip = {}
      for t, c in pairs(players_choice) do
        table.insert(target_tip,{t.id, c})
      end
      local ps = room:askToChoosePlayers(player,{
        targets = room:getOtherPlayers(player),
        min_num = 2,
        max_num = 2,
        skill_name = skel.name,
        prompt = "#sj_zhoushuhuizhan__qianheanyiting_change",
        cancelable = true,
        target_tip_name = skel.name,
        extra_data = {players_choice = target_tip},
      })
      if #ps == 2 then
        room:loseHp(player, 1, skel.name)
        local choice = players_choice[ps[1]]
        players_choice[ps[1]] = players_choice[ps[2]]
        players_choice[ps[2]] = choice
      end
    end
    for _, target in ipairs(room:getOtherPlayers(player)) do
      if players_choice[target] and not target.dead then
        local choice = players_choice[target]
        if choice == "#sj_zhoushuhuizhan__qianheanyiting_1" then
          target:drawCards(2,skel.name)
          target:turnOver()
        else
          room:askToDiscard(target, {
            min_num = 2,
            max_num = 2,
            include_equip = true,
            skill_name = skel.name,
            cancelable = false,
          })
          DIY.enterHidden(target)
        end
      end
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  cwh_diy.setReady(player,skel.name,math.max(0,cwh_diy.getReadyNum(player,skel.name)))--暂定不因获得/失去技能而重置
  --cwh_diy.setReady(player,skel.name)
end)

return skel